<template>
  <div>
    <el-input type="file" @change.native="importf($event)"></el-input>
    <el-button @click="xlsxFormatter(postdata,datalist)">甲供材资料</el-button>
  </div>
</template>
<script>
import XLSX from "xlsx";
import axios from "axios";
export default {
  props: {
    posturl: {
      type: String,
      default: ""
    },
    regionid: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      datalist: [],
      starting: false,
      xmmclist: []
    };
  },
  methods: {
    importf(obj) {
      let _this = this;
      var wb; //读取完成的数据
      var rABS = false; //是否将文件读取为二进制字符串
      var datajson = new Array();
      var updata;
      obj = obj.target;
      if (!obj.files) {
        return;
      }
      var f = obj.files[0];
      var reader = new FileReader();
      reader.onload = function(e) {
        var data = e.target.result;
        if (rABS) {
          wb = XLSX.read(btoa(fixdata(data)), {
            //手动转化
            type: "base64"
          });
        } else {
          wb = XLSX.read(data, {
            type: "binary"
          });
        }
        //wb.SheetNames[0]是获取Sheets中第一个Sheet的名字
        //wb.Sheets[Sheet名]获取第一个Sheet的数据
        datajson = XLSX.utils.sheet_to_json(wb.Sheets[wb.SheetNames[1]]);
        // _this.xlsxFormatter(datajson);
        _this.datalistCopy = JSON.parse(JSON.stringify(datajson));
        _this.ygformatter(datajson);
        // _this.datafomatter()
      };
      if (rABS) {
        reader.readAsArrayBuffer(f);
      } else {
        reader.readAsBinaryString(f);
      }
      //
    },
    xlsxFormatter(fn, data) {
      let _this = this;
      if (_this.starting) {
        return false;
      }
      if (data.length === 0) {
        this.$message({
          message: "未获得数据，无法上传",
          type: "warning"
        });
        return false;
      }
      _this.starting = true;
      _this.splitpost(fn, data, 1, 100);
    },
    ygformatter(datalist) {
      let xmmc = datalist.shift();
      xmmc = Object.values(xmmc)[0].split(":")[1];
      let keylist = datalist.shift();
      if (!this.xmmclist.includes(xmmc)) {
        this.xmmclist.push(xmmc);
        this.datalist.push({
          xmmc,
          regionid: _this.regionid,
          list: datalist.map(item => {
            return {
              wlmc: item.__EMPTY,
              ggxh: item.__EMPTY_1,
              dw: item.__EMPTY_2,
              sl: item.__EMPTY_3,
              dj: item.__EMPTY_4,
              hj: item.__EMPTY_5,
              jhr: item.__EMPTY_6,
              jhdd: item.__EMPTY_7,
              jhylsj: item.__EMPTY_8,
              remark: item.__EMPTY_9
            };
          })
        });
      }
    },
    postdata(list) {
      let _this = this;
      this.$axios({
        method: "post",
        url: _this.postdata,
        data: {
          list
        }
      })
        .then(response => {})
        .catch(error => {});
    },
    splitpost(fn, data, k, time) {
      let _this = this;
      if (data.length == 0) {
        _this.starting = false;
        return false;
      } else {
        fn(data.splice(0, k));
        setTimeout(function() {
          return _this.splitpost(fn, data, k, time);
        }, time);
      }
    }
  }
};
</script>
